/**
 * 登录页
 */
package com.xiaomuwu.android.ui.login

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.xiaomuwu.android.MainActivity
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.logic.Repository
import com.xiaomuwu.android.logic.model.ForCheckLogin
import com.xiaomuwu.android.logic.model.ForOperateUser
import com.xiaomuwu.android.ui.LoginStatus
import com.xiaomuwu.android.ui.home.ContentFragment
import com.xiaomuwu.android.ui.Displayer
import kotlinx.android.synthetic.main.login_fragment.*

class LoginFragment:Fragment(){

    private val viewModel by lazy{ ViewModelProviders.of(this).get(
        LoginViewModel::class.java)}

    private val forCheckLogin:ForCheckLogin=ForCheckLogin(null,null)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.login_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        when(Displayer.pageStatus) {
            // 根据页面设置相关控件显示状态和提示信息
            "Login"->{
                idEditText.visibility=View.VISIBLE  // 显示账号输入框
                logIn.visibility=View.VISIBLE   // 显示 “登录” 按钮
                register.visibility=View.VISIBLE    // 显示 “注册” 按钮
            }
            "ModifyUser"->{
                nameEditText.hint="昵称"  // 设置昵称输入框的提示文字
                modifyUser.visibility=View.VISIBLE  // 显示 “修改” 按钮
            }
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        // 根据页面设置监听
        if(Displayer.pageStatus=="Login") {
            viewModel.loginResultLiveData.observe(viewLifecycleOwner) { loginResultLiveData ->
                // 根据数据库返回的结果设置相关数据并显示提示信息
                when (loginResultLiveData.getOrNull().toString()) {
                    "NoUser" -> {
                        LoginStatus.user_ID = null
                        Toast.makeText(XiaoMuWuApplication.context, "该账号不存在",
                            Toast.LENGTH_SHORT).show()
                    }
                    "0" -> {
                        LoginStatus.user_ID = null
                        Toast.makeText(XiaoMuWuApplication.context, "密码错误",
                            Toast.LENGTH_SHORT).show()
                    }
                    "1" -> {
                        LoginStatus.user_ID = forCheckLogin.userID
                        Toast.makeText(XiaoMuWuApplication.context, "登录成功",
                            Toast.LENGTH_SHORT).show()

                        // 切换至首页
                        if (activity != null) {
                            val mainActivity = activity as MainActivity
                            Displayer.pprevPageStatus=Displayer.prevPageStatus
                            Displayer.prevPageStatus=Displayer.pageStatus
                            Displayer.pageStatus = "Home"
                            mainActivity.hideKeyboard() // 收起键盘
                            mainActivity.replaceFragment(ContentFragment())
                        }
                    }
                }
            }
            viewModel.registerResultLiveData.observe(viewLifecycleOwner) { registerResultLiveData ->
                // 根据数据库返回的结果设置相关数据并显示提示信息
                when (registerResultLiveData.getOrNull().toString()) {
                    "ExistUser" -> {
                        Toast.makeText(XiaoMuWuApplication.context, "改账号已存在！",
                            Toast.LENGTH_SHORT).show()
                    }
                    "1" -> {
                        Toast.makeText(XiaoMuWuApplication.context, "注册成功！",
                            Toast.LENGTH_SHORT).show()
                    }
                }
            }

            // “登录” 按钮点击监听
            logIn?.setOnClickListener {
//            val imm = getSystemService<Any>(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
//            imm?.hideSoftInputFromWindow(view!!.windowToken, 0)

                // 登录检查
                forCheckLogin.userID = idEditText.text.toString()
                forCheckLogin.password = psdEditText.text.toString()
                viewModel.getLoginResultLiveData(forCheckLogin)

//            when(Repository.checkLogin(userID,password)){
//                "NoUser"->{
//                    Displayer.user_ID=null
//                    Toast.makeText(XiaoMuWuApplication.context, "该账号不存在！", Toast.LENGTH_SHORT).show()
//                }
//                "0"->{
//                    Displayer.user_ID=null
//                    Toast.makeText(XiaoMuWuApplication.context, "密码错误！", Toast.LENGTH_SHORT).show()
//                }
//                "1"->{
//                    Displayer.user_ID=userID
//                    Toast.makeText(XiaoMuWuApplication.context, "登录成功！", Toast.LENGTH_SHORT).show()
//                }
//            }
//            if(activity!=null){
//                val mainActivity=activity as MainActivity
//                if(Displayer.user_ID!=null){
//                    mainActivity.replaceFragment(ContentFragment())
//                    mainActivity.hideKeyboard()   // 收起键盘
//                }
//            }

            }

            // “注册” 按钮点击监听
            register.setOnClickListener {
                if (idEditText.text.isBlank()) {
                    Toast.makeText(XiaoMuWuApplication.context, "请输入账号！",
                        Toast.LENGTH_SHORT).show()
                } else {
                    if (psdEditText.text.isBlank()) {
                        Toast.makeText(XiaoMuWuApplication.context, "请输入密码！",
                            Toast.LENGTH_SHORT).show()
                    } else {
                        if (nameEditText.text.isBlank()) {
                            Toast.makeText(XiaoMuWuApplication.context, "请输入昵称！",
                                Toast.LENGTH_SHORT).show()
                        } else {
                            // 注册账号
                            viewModel.getRegisterResultLiveData(
                                ForOperateUser(
                                    idEditText.text.toString(), nameEditText.text.toString(),
                                    psdEditText.text.toString(),"insert"
                                )
                            )
                        }
                    }
                }
            }
        }else if(Displayer.pageStatus=="ModifyUser") {
            // “修改” 按钮点击监听
            modifyUser.setOnClickListener {
                if(nameEditText.text.isBlank()){
                    Toast.makeText(XiaoMuWuApplication.context, "请输入昵称！",
                        Toast.LENGTH_SHORT).show()
                }else{
                    if(psdEditText.text.isBlank()){
                        Toast.makeText(XiaoMuWuApplication.context, "请输入密码！",
                            Toast.LENGTH_SHORT).show()
                    }else{
                        // 修改用户信息
                        Repository.modigyUser(ForOperateUser(LoginStatus.user_ID!!,
                            nameEditText.text.toString(), psdEditText.text.toString(),
                            "update"))
                        Toast.makeText(XiaoMuWuApplication.context,"修改成功",Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }

}